package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.entity.DataDetialEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class HomeEliteListViewAdapter extends BaseAdapter{
    private Context context;
    private List<DataDetialEty> datas;

    public HomeEliteListViewAdapter(Context context, List<DataDetialEty> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (null == convertView){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_elite,null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (MyViewHolder) convertView.getTag();
        }
        String date = datas.get(position).getDatetime();
        date = date.substring(5,10);
        holder.dataTimeTv.setText(date);
        holder.titleTv.setText(datas.get(position).getTitle());
        holder.urlIv.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.urlIv.setErrorImageResId(R.mipmap.ic_launcher);
        String url = datas.get(position).getImage_list().get(0).getUrl();

        holder.urlIv.setImageUrl(url, VolleySingleton.getImageLoader());
        return convertView;
    }
    public void refreshData(List<DataDetialEty> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    class MyViewHolder{
        NetworkImageView urlIv;
        TextView dataTimeTv;
        TextView titleTv;

        public MyViewHolder(View itemView) {
            dataTimeTv = (TextView) itemView.findViewById(R.id.tv_datetime);
            titleTv = (TextView) itemView.findViewById(R.id.tv_title);
            urlIv = (NetworkImageView) itemView.findViewById(R.id.iv_url);

        }
    }
}
